package com.da.boot.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.da.boot.bean.TraxSummaryExcelExport;
import com.da.boot.util.EasyExcelUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JokerDa.
 * @date 2021/6/16 17:02
 * Description:
 */
@RestController
@RequestMapping(value = {"test1", "test2"})
public class HelloController {
    private String data =
        "[{'area':'北京市','regularItemRate':'9.09%','formatName':'便利店','heapRate':'56.48%',"
            + "'coreItemRate':'56.67%','totalOfImportsItemNum':2,'freezerRate':'30.97%','totalRate':'38"
            + ".86%','bu':'北区','totalOfCustomers':55,'totalOfNotImportsItemNum':5,'channelName':'KA',"
            + "'goodsShelvesRate':'36.86%','region':'北京市'},{'area':'北京市','regularItemRate':'11.29%',"
            + "'formatName':'超市','heapRate':'29.46%','coreItemRate':'48.71%','totalOfImportsItemNum':2,"
            + "'freezerRate':'52.20%','totalRate':'28.96%','bu':'北区','totalOfCustomers':30,"
            + "'totalOfNotImportsItemNum':7,'channelName':'KA','goodsShelvesRate':'27.69%',"
            + "'region':'北京市'},"
            + "{'area':'北京市','regularItemRate':'4.05%','formatName':'大卖场','heapRate':'30.24%',"
            + "'coreItemRate':'76.72%','totalOfImportsItemNum':4,'freezerRate':'0.00%','totalRate':'30"
            + ".29%','bu':'北区','totalOfCustomers':35,'totalOfNotImportsItemNum':19,'channelName':'KA',"
            + "'goodsShelvesRate':'28.85%','region':'北京市'},{'area':'北京市','regularItemRate':'0.00%',"
            + "'formatName':'社会超市','heapRate':'0.00%','coreItemRate':'6.25%','totalOfImportsItemNum':0,"
            + "'freezerRate':'0.00%','totalRate':'6.67%','bu':'北区','totalOfCustomers':2,"
            + "'totalOfNotImportsItemNum':1,'channelName':'KA','goodsShelvesRate':'6.67%',"
            + "'region':'北京市'}]";

    @GetMapping(value = "/hello/{name}")
    public String hello(@PathVariable String name) {
        return "Hello " + name;
    }

    @GetMapping("/download")
    public void downloadTraxSummary(HttpServletResponse httpServletResponse) {
        List<TraxSummaryExcelExport> list = JSONUtil.toList(data, TraxSummaryExcelExport.class);
        String sheetName = "sheet1";
        String fileName = "图像识别区域汇总报表" + DateUtil.format(new Date(), "yyyyMMddHHMMSS") + ".xlsx";
        EasyExcelUtil.writeExcel(httpServletResponse, list, fileName, sheetName, TraxSummaryExcelExport.class);
    }
}
